package com.zking.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.zking.dao.AddressDao;
import com.zking.dao.EvaluateDao;
import com.zking.dao.FoodDao;
import com.zking.dao.OrderDao;
import com.zking.entity.Address;
import com.zking.entity.Evaluate;
import com.zking.entity.Food;
import com.zking.entity.Order;
import com.zking.frame.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

import javax.swing.*;
import javax.swing.border.MatteBorder;

//开始页面

public class Tool工具 extends JFrame {

    private volatile static Map<String, Integer> map;
    private volatile static JPanel Orderj,jPanel1;

    private static JPanel jPanel2;
    private static JLabel jLabel4;
    private volatile static String name;
    private volatile static List<Address> addresses;
    private static JScrollPane inJScrollPane,inJScrollPane1,jScrollPane;
    private volatile static boolean sendSms = true;


    public void setOrderj(JPanel jPanel2){
        Orderj = jPanel2;
    }
    public JPanel getOrderj(){
        return Orderj;
    }

    public void setName(String name){
        System.out.println(name);
        this.name=name;
    }
    public String getName(){
        return name;
    }

    public static boolean getsendSms() {
        return sendSms;
    }

    public static void setSendSms(boolean sendSms) {
        Tool工具.sendSms = sendSms;
    }

    // 获取主面板
    public static JFrame getJFrame() {
        JFrame jFrame = new JFrame();
        // 设置名字
        jFrame.setTitle("吃了没");
        // 设置背景颜色
        jFrame.getContentPane().setBackground(Color.white);
        // 设置窗口大小
        jFrame.setSize(650, 1000);
        // 取消布局
        jFrame.setLayout(null);
        // 设置居中
        jFrame.setLocationRelativeTo(null);
        // 设置窗口不可调节s
        jFrame.setResizable(false);
        // 设置窗口是否可见
        jFrame.setVisible(true);
        // 设置窗口关闭模式
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        return jFrame;
    }

    // 获取首页页面JPanelHomePage
    public static JPanel getJPanelHomePage(JFrame jFrame) {
        JPanel jPanel, jPanel2;
        JLabel simpleLunchJLabel, fruitsAndVegetablesJLabel, sweetMilkTeaJLabel, dinnerNightJLabel, jLabel;// 四种分类
        JScrollPane inJScrollPane;// 滚动窗口

        jFrame.getContentPane().setBackground(Color.white);
        jPanel = new JPanel();
        jPanel.setBounds(10, 100, 615, 800);
        jPanel.setBorder(new MatteBorder(2, 2, 2, 2, new Color(77, 248, 36)));
        jPanel.setBackground(new Color(114, 248, 81));
        jPanel.setLayout(null);

        // 设置背景图片
        ImageIcon icon = new ImageIcon(HomePage.class.getResource("01.jpg"));
        jLabel = new JLabel(icon);
        jLabel.setBounds(0, 0, 615, 60);
        jPanel.add(jLabel, -1);

        // 设置四大分类
        simpleLunchJLabel = new JLabel("简餐便当", 0);
        simpleLunchJLabel.setBounds(40, 5, 100, 50);
        simpleLunchJLabel.setFont(new Font("黑体", Font.BOLD, 20));
        simpleLunchJLabel.setBorder(new MatteBorder(2, 2, 2, 2, new Color(254, 253, 129)));
        simpleLunchJLabel.setOpaque(true);
        simpleLunchJLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        // 设置点击事件
        simpleLunchJLabel.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    new SweetMilkTea(jFrame, "简餐便当");
                }
            }
        });
        jPanel.add(simpleLunchJLabel, 0);

        fruitsAndVegetablesJLabel = new JLabel("水果蔬菜", 0);
        fruitsAndVegetablesJLabel.setBounds(190, 5, 100, 50);
        fruitsAndVegetablesJLabel.setFont(new Font("黑体", Font.BOLD, 20));
        fruitsAndVegetablesJLabel.setBorder(new MatteBorder(2, 2, 2, 2, new Color(254, 253, 129)));
        fruitsAndVegetablesJLabel.setOpaque(true);
        fruitsAndVegetablesJLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        // 设置点击事件
        fruitsAndVegetablesJLabel.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    new SweetMilkTea(jFrame, "水果蔬菜");
                }
            }
        });
        jPanel.add(fruitsAndVegetablesJLabel, 0);

        sweetMilkTeaJLabel = new JLabel("甜品奶茶", 0);
        sweetMilkTeaJLabel.setBounds(340, 5, 100, 50);
        sweetMilkTeaJLabel.setFont(new Font("黑体", Font.BOLD, 20));
        sweetMilkTeaJLabel.setBorder(new MatteBorder(2, 2, 2, 2, new Color(254, 253, 129)));
        sweetMilkTeaJLabel.setOpaque(true);
        sweetMilkTeaJLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        // 设置点击事件
        sweetMilkTeaJLabel.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    new SweetMilkTea(jFrame, "甜品奶茶");
                }
            }
        });
        jPanel.add(sweetMilkTeaJLabel, 0);

        dinnerNightJLabel = new JLabel("晚餐宵夜", 0);
        dinnerNightJLabel.setBounds(490, 5, 100, 50);
        dinnerNightJLabel.setFont(new Font("黑体", Font.BOLD, 20));
        dinnerNightJLabel.setBorder(new MatteBorder(2, 2, 2, 2, new Color(254, 253, 129)));
        dinnerNightJLabel.setOpaque(true);
        dinnerNightJLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        // 设置点击事件
        dinnerNightJLabel.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    new SweetMilkTea(jFrame, "晚餐宵夜");
                }
            }
        });
        jPanel.add(dinnerNightJLabel, 0);

        // 设置中部的滚动面板
        jPanel2 = new JPanel();

        jPanel2.setPreferredSize(new Dimension(590, 145 * 12 + 20)); // 设定面板大小
        jPanel2.setLayout(null); // 取消布局
        inJScrollPane = new JScrollPane(jPanel2);// 设定滚动窗口并加入面板
        inJScrollPane.setBounds(0, 60, 615, 740);
        jPanel.add(inJScrollPane);// 将滚动窗口加入到主窗口

        // 推出商家
        List<Food> foods = new ArrayList<Food>();
        FoodDao foodDao = new FoodDao();
        foods = foodDao.queryfood();
        for (int a = 1;a<5;a++){
            switch (a){
                case 1:
                    setShop(jFrame,jPanel2,foods,"简餐便当",0);
                    break;
                case 2:
                    setShop(jFrame,jPanel2,foods,"水果蔬菜",1);
                    break;
                case 3:
                    setShop(jFrame,jPanel2,foods,"甜品奶茶",2);
                    break;
                case 4:
                    setShop(jFrame,jPanel2,foods,"晚餐宵夜",3);
                    break;
            }
        }

        return jPanel;
    }

    // 获取订单页面JPanelOrder
    public static JPanel getJPanelOrder(JFrame jFrame) {
        JPanel jPanel;
        JLabel jLabel, jLabel2;

        // 设置面板
        jFrame.getContentPane().setBackground(Color.white);
        jPanel = new JPanel();
        jPanel.setBounds(10, 100, 615, 800);
        jPanel.setLayout(null);
        Tool工具 tool工具 = new Tool工具();
        OrderDao orderDao = new OrderDao();
        List<Order> orders =orderDao.queryOrder(tool工具.getName());
        jPanel1 = new JPanel();



        // 设置背景图片
        ImageIcon icon = new ImageIcon(HomePage.class.getResource("01.jpg"));
        jLabel = new JLabel(icon);
        jLabel.setBounds(0, 0, 615, 60);
        jPanel.add(jLabel, -1);

        // 设置两个
        jLabel = new JLabel("外卖订单", 0);
        jLabel.setBounds(0, 0, 615 / 2, 60);
        jLabel.setFont(new Font("黑体", Font.BOLD, 30));
        jLabel.setBorder(new MatteBorder(2, 2, 2, 2, new Color(254, 253, 129)));
        jLabel.setBackground(Color.white);
        jLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        jPanel.add(jLabel, 0);

        jLabel2 = new JLabel("待评价", 0);
        jLabel2.setBounds(615 / 2, 0, 615 / 2, 60);
        jLabel2.setFont(new Font("黑体", Font.BOLD, 30));
        jLabel2.setBorder(new MatteBorder(2, 2, 2, 2, new Color(254, 253, 129)));
        jLabel2.setBackground(Color.white);
        jLabel2.setCursor(new Cursor(Cursor.HAND_CURSOR));
        jPanel.add(jLabel2, 0);
        jLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount()==1){
                    jPanel1=Tool工具.getJPanelOrders(jFrame);
                    inJScrollPane.setViewportView(jPanel1);
                    // 加载并刷新面板
                    inJScrollPane.repaint();
                    inJScrollPane.updateUI();
                    inJScrollPane.revalidate();
                }
            }
        });
        jLabel2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount()==1){
                    jPanel1=Tool工具.getJPanelEvaluate(jFrame);
                    inJScrollPane.setViewportView(jPanel1);
                    // 加载并刷新面板
                    inJScrollPane.repaint();
                    inJScrollPane.updateUI();
                    inJScrollPane.revalidate();
                }
            }
        });

        jPanel1 = Tool工具.getJPanelOrders(jFrame);
        inJScrollPane = new JScrollPane(jPanel1);// 设定滚动窗口并加入面板
        inJScrollPane.setBounds(0, 60, 615, 740);
        jPanel.add(inJScrollPane, 0);// 将滚动窗口加入到主窗口

        return jPanel;
    }

    /* 获取个人信息页面JPanelMy */
    public static JPanel getJPanelMy(JFrame jFrame,String name) {

        // 设置面板
        jFrame.getContentPane().setBackground(Color.white);
        JPanel jPanel = new JPanel();
        jPanel.setBounds(10, 100, 615, 800);
        jPanel.setBackground(Color.white);
        jPanel.setLayout(null);

        // 设置背景图片
        ImageIcon icon1 = new ImageIcon(HomePage.class.getResource("01.jpg"));
        JLabel jLabel1 = new JLabel(icon1);
        jLabel1.setBounds(0, 0, 615, 60);
        jPanel.add(jLabel1, -1);

        // 设置头像
        ImageIcon icon = new ImageIcon(HomePage.class.getResource("我的头像.jpg"));
        JLabel jLabel = new JLabel(icon);
        jLabel.setBounds(0, 0, 90, 125);
        jPanel.add(jLabel, 0);

        // 设置存储个人信息的标签
        Tool工具 tool工具 = new Tool工具();
        JLabel namejLabel = new JLabel(tool工具.getName());
        namejLabel.setBounds(90, 5, 200, 50);
        namejLabel.setFont(new Font("黑体", Font.BOLD, 20));
        jPanel.add(namejLabel, 0);

        JLabel epigramjJLabel = new JLabel("不论成功还是失败，都是系于自己。");
        epigramjJLabel.setBounds(90, 60, 300, 50);
        epigramjJLabel.setFont(new Font("黑体", Font.BOLD, 15));
        jPanel.add(epigramjJLabel, 0);

        // 书写地址内容
        JLabel addressjLabel = new JLabel("收货地址", 0);
        addressjLabel.setBounds(0, 140, 615 / 2, 50);
        addressjLabel.setFont(new Font("黑体", Font.BOLD, 30));
        addressjLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        addressjLabel.setBorder(new MatteBorder(2, 2, 2, 2, new Color(188, 188, 188)));
        jPanel.add(addressjLabel, 0);


        JLabel addToAddressjJLabel = new JLabel("添加地址", 0);
        addToAddressjJLabel.setBounds(615 / 2, 140, 615 / 2, 50);
        addToAddressjJLabel.setFont(new Font("黑体", Font.BOLD, 30));
        addToAddressjJLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        addToAddressjJLabel.setBorder(new MatteBorder(2, 2, 2, 2, new Color(188, 188, 188)));
        jPanel.add(addToAddressjJLabel, 0);

        //设置点击事件，加入子面板
        jPanel2 = new JPanel();
        jPanel2 = Tool工具.getAddressJPanel(jFrame,name);

        addToAddressjJLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount()==1){
                    new AddSite(jFrame,true,"添加地址",new Address());
                    jPanel2=Tool工具.getAddressJPanel(jFrame,name);
                    inJScrollPane1.setViewportView(jPanel2);
                    // 加载并刷新面板
                    inJScrollPane1.repaint();
                    inJScrollPane1.updateUI();
                    inJScrollPane1.revalidate();
                }
            }
        });

        // 设置滚动面板
        inJScrollPane1 = new JScrollPane(jPanel2);// 设定滚动窗口并加入面板
        inJScrollPane1.setBounds(0, 190, 615, 610);
        jPanel.add(inJScrollPane1, 0);// 将滚动窗口加入到主窗口

        return jPanel;
    }

    // 获取四大分类商铺图片面板
    public static JPanel getjpShops(String sort, JFrame jFrame) {

        JPanel jPanel = new JPanel();
        jPanel.setLayout(null);
        jPanel.setBounds(0, 0, 615, 1600);
        List<Food> foods = new ArrayList<Food>();

        FoodDao foodDao = new FoodDao();
        foods = foodDao.queryFoodSort(sort);

        List<String> shop = new ArrayList<String>();
        for (int a = 0; a < foods.size(); a++) {
            JPanel jPanel1 = new JPanel();
            jPanel1.setLayout(null);
            jPanel1.setBackground(Color.white);
            if (foods.get(a).getSort().equals(sort)) {
                if(shop.size()==0){
                    shop.add(foods.get(a).getShop());
                    ImageIcon icona = new ImageIcon(foods.get(a).getStoreaddress());
                    JLabel jLabel1 = new JLabel(icona,0);
                    jLabel1.setBounds(0,0,200,140);
                    jPanel1.add(jLabel1);
                    JLabel jLabel2 = new JLabel(foods.get(a).getShop());
                    jLabel2.setBounds(200,10,400,60);
                    jLabel2.setFont(new Font("黑体",Font.BOLD,30));
                    jPanel1.add(jLabel2);
                    jPanel1.setBounds(0, 145 * ((shop.size()-1)), 600, 140);
                    jPanel.add(jPanel1);
                    jPanel1.setCursor(new Cursor(Cursor.HAND_CURSOR));
                    jPanel1.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            // TODO Auto-generated method stub
                            if (e.getClickCount() == 1) {
                                new Commodity(jFrame, sort, shop.get(0));
                            }
                        }
                    });
                }else{
                    boolean abcd=true;
                    for (int b = 0; b < shop.size(); b++) {
                        if (foods.get(a).getShop().equals(shop.get(b))) {
                            abcd = false;
                        }
                    }
                    if (abcd){
                        String shopName=foods.get(a).getShop();
                        shop.add(foods.get(a).getShop());
                        ImageIcon icona = new ImageIcon(foods.get(a).getStoreaddress());
                        JLabel jLabel1 = new JLabel(icona,0);
                        jLabel1.setBounds(0,0,200,140);
                        jPanel1.add(jLabel1);
                        JLabel jLabel2 = new JLabel(foods.get(a).getShop());
                        jLabel2.setBounds(200,10,400,60);
                        jLabel2.setFont(new Font("黑体",Font.BOLD,30));
                        jPanel1.add(jLabel2);
                        jPanel1.setBounds(0, 145 * (shop.size()-1), 600, 140);
                        jPanel1.setCursor(new Cursor(Cursor.HAND_CURSOR));
                        jPanel.add(jPanel1);
                        jPanel1.addMouseListener(new MouseAdapter() {
                            @Override
                            public void mouseClicked(MouseEvent e) {
                                // TODO Auto-generated method stub
                                if (e.getClickCount() == 1) {
                                    new Commodity(jFrame, sort, shopName);
                                }
                            }
                        });
                    }

                }
            }
        }

        return jPanel;
    }

    // 获取商铺菜品图片面板
    public static JPanel getjpFoods(String sort, String shop, JFrame jFrame) {
        //清空上一次点单的商品
        map = new HashMap<String , Integer>();
        List<Food> foods = new ArrayList<Food>();
        FoodDao foodDao = new FoodDao();
        foods = foodDao.queryFoodName(sort,shop);

        // 显示购买的商品
        JLabel jLabel5;
        JPanel jPanel1;
        // 创建map集合存储用户购买的商品
        Map<String, Integer> map = new HashMap<String, Integer>();

        JPanel jPanel = new JPanel();
        jPanel.setLayout(null);
        jPanel.setBounds(0, 0, 615, 900);


        jPanel1 = new JPanel();
        jPanel1.setBounds(0, 830, 615, 70);
        jPanel1.setLayout(null);
        jPanel1.setBackground(Color.white);
        jPanel1.setBorder(new MatteBorder(2, 2, 2, 2, new Color(77, 248, 36)));

        // 存储用户点击的商品
        jLabel5 = new JLabel();
        jLabel5.setBounds(0,10,400,50);
        jLabel5.setFont(new Font("黑体",Font.BOLD,20));
        jLabel5.setCursor(new Cursor(Cursor.HAND_CURSOR));
        jPanel1.add(jLabel5);

        JPanel jpanel = new JPanel();
        jpanel = getJpaneShop(shop,sort,jLabel5);
        jScrollPane = new JScrollPane(jpanel);// 滚动框面板
        jScrollPane.setBounds(0, 120, 615, 710);
        jPanel.add(jScrollPane, 0);

        JLabel jLabel1 = new JLabel("商品", 0);
        jLabel1.setFont(new Font("黑体", Font.BOLD, 20));
        jLabel1.setBounds(0, 90, 615 / 2, 30);
        jLabel1.setCursor(new Cursor(Cursor.HAND_CURSOR));
        jLabel1.setBorder(new MatteBorder(2, 2, 2, 2, new Color(254, 253, 129)));
        // 设置点击事件
        jLabel1.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                JPanel jPanel1 = new JPanel();
                jPanel1=getJpaneShop(shop,sort,jLabel5);
                jScrollPane.setViewportView(jPanel1);
                // 加载并刷新面板
                jScrollPane.repaint();
                jScrollPane.updateUI();
                jScrollPane.revalidate();
            }
        });
        jPanel.add(jLabel1, 0);

        JLabel jLabel2 = new JLabel("评价", 0);
        jLabel2.setFont(new Font("黑体", Font.BOLD, 20));
        jLabel2.setBounds(615 / 2, 90, 615 / 2, 30);
        jLabel2.setCursor(new Cursor(Cursor.HAND_CURSOR));
        jLabel2.setBorder(new MatteBorder(2, 2, 2, 2, new Color(254, 253, 129)));
        // 设置点击事件
        jLabel2.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                JPanel jPanel3 = new JPanel();
                jPanel3 = getJpaneEvaluate(shop);
                jScrollPane.setViewportView(jPanel3);
                // 加载并刷新面板
                jScrollPane.repaint();
                jScrollPane.updateUI();
                jScrollPane.revalidate();
            }
        });
        jPanel.add(jLabel2, 0);

        ImageIcon icon = new ImageIcon(foods.get(0).getShow());
        JLabel jLabel3 = new JLabel(icon);
        jLabel3.setBounds(0, 0, 615, 90);
        jPanel.add(jLabel3);

        // 查看map集合的
        Tool工具 tool工具 = new Tool工具();
        jLabel5.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount()==1){
                    jLabel5.setText("");
                    new Wares(jFrame,true,"你点了以下商品",tool工具.getMap(),sort,shop);
                    String abc="";
                    for (String naem:tool工具.getMap().keySet()){
                        abc=naem;
                    }
                    jLabel5.setText(abc);
                    setJLabel4(shop,sort);
                }
            }
        });

        // 设置提交订单事件
        JLabel jLabel = new JLabel("确认订单",
                0);
        jLabel.setFont(new Font("黑体", Font.BOLD, 20));
        jLabel.setBounds(500, 10, 100, 50);
        jLabel.setBorder(new MatteBorder(2, 2, 2, 2, new Color(77, 248, 36)));
        jLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        jPanel1.add(jLabel);
        // 设置点击事件
        jLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount()==1){
                    addresses = new ArrayList<Address>();
                    AddressDao a = new AddressDao();
                    addresses = a.queryStudents(name);
                    if (addresses.size()!=0){
                        new Orderj(jFrame,true,tool工具.getMap(),addresses,sort,shop);
                    }else{JOptionPane.showMessageDialog(jFrame,"你还没有设置收货地址，请前往《我的》去设置地址");}

                }
            }
        });

        // 存储价格
        jLabel4 = new JLabel("￥"+0,4);
        jLabel4.setBounds(300,10,200,50);
        jLabel4.setFont(new Font("黑体",Font.BOLD,20));
        jLabel4.setForeground(Color.RED);
        jPanel1.add(jLabel4);


        jPanel.add(jPanel1);

        return jPanel;
    }

    // 添加map集合参数
    public void setMap(String shop, String sort, String name, JLabel jLabel1){
        // 定义所需要的变量
        List<Food> foods = new ArrayList<Food>();

        FoodDao foodDao = new FoodDao();
        // 获取商店菜品
        foods = foodDao.queryFoodName(sort,shop);


        // 获取点击菜品
        for (int i=0;i<foods.size();i++){
            if (foods.get(i).getName().equals(name)){
                jLabel1.setText(name);

                // 获取map集合所以的kei
                if (map.keySet().size()==0){
                    map.put(name,1);
                }else{
                    int ab=0;
                    for(String key : map.keySet()){
                        if (key.equals(name)){
                            map.put(name,map.get(name)+1);
                            ab++;
                            return;
                        }
                    }
                    if(ab==0){
                        map.put(name,1);
                    }
                    ab=0;
                }
            }
        }


    }

    // 获取Map集合，（存已点击的菜品）
    public Map<String, Integer> getMap(){
        return map;
    }

    // 显示价格，（已点击的菜品总价格）
    public static void setJLabel4 (String shop,String sort){
        double money=0.0;

        FoodDao foodDao = new FoodDao();
        // 获取商店菜品
        List<Food> fonts = foodDao.queryFoodName(sort,shop);

        for(String key:map.keySet()){
            for (int ab = 0;ab<fonts.size();ab++){
                if (fonts.get(ab).getName().equals(key)){
                    money += fonts.get(ab).getPrice()*map.get(key);
                    break;
                }
            }
        };
        DecimalFormat df1 = new DecimalFormat("####.00");
        jLabel4.setText("￥"+df1.format(money));
    }

    // 获取商店名
    public static void setShop(JFrame jFrame,JPanel jPanel2,List<Food> foods,String sort,int in){
        List<String> shop = new ArrayList<String>();
        for (int a = 0; a < foods.size(); a++) {
                JPanel jPanel1 = new JPanel();
                jPanel1.setLayout(null);
                jPanel1.setBackground(Color.white);
                if (foods.get(a).getSort().equals(sort)) {
                    if(shop.size()==0){
                        shop.add(foods.get(a).getShop());
                        ImageIcon icona = new ImageIcon(foods.get(a).getStoreaddress());
                        JLabel jLabel1 = new JLabel(icona,0);
                        jLabel1.setBounds(0,0,200,140);
                        jPanel1.add(jLabel1);
                        JLabel jLabel2 = new JLabel(foods.get(a).getShop());
                        jLabel2.setBounds(200,10,400,60);
                        jLabel2.setFont(new Font("黑体",Font.BOLD,30));
                        jPanel1.add(jLabel2);
                        jPanel1.setBounds(0, 145 * ((shop.size()-1)+(3*in)), 600, 140);
                        jPanel2.add(jPanel1);
                        jPanel1.setCursor(new Cursor(Cursor.HAND_CURSOR));
                        jPanel1.addMouseListener(new MouseAdapter() {
                            @Override
                            public void mouseClicked(MouseEvent e) {
                                // TODO Auto-generated method stub
                                if (e.getClickCount() == 1) {
                                    new Commodity(jFrame, sort, shop.get(0));
                                }
                            }
                        });
                    }else{
                        boolean abcd=true;
                        for (int b = 0; b < shop.size(); b++) {
                            if (foods.get(a).getShop().equals(shop.get(b))) {
                                abcd = false;
                            }
                        }
                        if (abcd){
                            shop.add(foods.get(a).getShop());
                            ImageIcon icona = new ImageIcon(foods.get(a).getStoreaddress());
                            JLabel jLabel1 = new JLabel(icona,0);
                            jLabel1.setBounds(0,0,200,140);
                            jPanel1.add(jLabel1);
                            JLabel jLabel2 = new JLabel(foods.get(a).getShop());
                            jLabel2.setBounds(200,10,400,60);
                            jLabel2.setFont(new Font("黑体",Font.BOLD,30));
                            jPanel1.add(jLabel2);
                            jPanel1.setBounds(0, 145 * ((shop.size()-1)+(3*in)), 600, 140);
                            jPanel1.setCursor(new Cursor(Cursor.HAND_CURSOR));
                            jPanel2.add(jPanel1);
                            jPanel1.addMouseListener(new MouseAdapter() {
                                @Override
                                public void mouseClicked(MouseEvent e) {
                                    // TODO Auto-generated method stub
                                    if (e.getClickCount() == 1) {
                                        new Commodity(jFrame, sort, shop.get(shop.size()-1));
                                    }
                                }
                            });
                        }

                }
            }
                if(shop.size()==3){

                    return;
                }

        }
    }

    // 显示收货地址
    public static JPanel getAddressJPanel(JFrame jFrame,String username){
        addresses = new ArrayList<Address>();
        AddressDao a = new AddressDao();
        addresses = a.queryStudents(username);
        JPanel jPanel = new JPanel();
        jPanel.setBounds(0,0,590, 640);
        jPanel.setLayout(null);

        jPanel.setPreferredSize(new Dimension(590,10+150*addresses.size()));

        for (int i=0;i<addresses.size();i++){
            JPanel jPanel1 = new JPanel();
            jPanel1.setBounds(0,10+150*i,590,140);
            jPanel1.setLayout(null);
            jPanel1.setBackground(Color.white);
            JLabel jLabel = new JLabel("地址：");
            jLabel.setBounds(10,10,80,60);
            jLabel.setForeground(Color.RED);
            jLabel.setFont(new Font("黑体",Font.BOLD,20));
            jPanel1.add(jLabel);
            JLabel jLabel1 = new JLabel(addresses.get(i).getAddress());
            jLabel1.setBounds(90,10,420,60);
            jLabel1.setFont(new Font("黑体",Font.BOLD,20));
            jPanel1.add(jLabel1);
            JLabel jLabel2 = new JLabel("姓名：");
            jLabel2.setBounds(10,80,80,60);
            jLabel2.setForeground(Color.RED);
            jLabel2.setFont(new Font("黑体",Font.BOLD,20));
            jPanel1.add(jLabel2);
            JLabel jLabel3 = new JLabel(addresses.get(i).getName());
            jLabel3.setBounds(90,80,130,60);
            jLabel3.setFont(new Font("黑体",Font.BOLD,20));
            jPanel1.add(jLabel3);
            JLabel jLabel4 = new JLabel("电话号码：");
            jLabel4.setBounds(220,80,120,60);
            jLabel4.setForeground(Color.RED);
            jLabel4.setFont(new Font("黑体",Font.BOLD,20));
            jPanel1.add(jLabel4);
            JLabel jLabel5 = new JLabel(addresses.get(i).getPhone());
            jLabel5.setBounds(340,80,150,60);
            jLabel5.setFont(new Font("黑体",Font.BOLD,20));
            jPanel1.add(jLabel5);

            JButton jButton = new JButton("修改");
            jButton.setBounds(490,10,80,50);
            jButton.setFont(new Font("黑体",Font.BOLD,20));
            jPanel1.add(jButton);

            JButton jButton1 = new JButton("删除");
            jButton1.setBounds(490,70,80,50);
            jButton1.setFont(new Font("黑体",Font.BOLD,20));
            jPanel1.add(jButton1);
            jPanel.add(jPanel1);
            /**
             * 删除地址
             */
            jButton1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    Address address = new Address();
                    address.setName(jLabel3.getText());
                    address.setAddress(jLabel1.getText());
                    address.setPhone(jLabel5.getText());
                    address.setUsername(username);
                    int a=new AddressDao().DeleteDddress(address);
                    if (a==1){
                        jPanel2=Tool工具.getAddressJPanel(jFrame,username);
                        inJScrollPane1.setViewportView(jPanel2);
                        // 加载并刷新面板
                        inJScrollPane1.repaint();
                        inJScrollPane1.updateUI();
                        inJScrollPane1.revalidate();
                        JOptionPane.showMessageDialog(jFrame, "删除成功", "成功", JOptionPane.OK_CANCEL_OPTION);
                    }else{
                        JOptionPane.showMessageDialog(jFrame, "删除失败", "失败", JOptionPane.OK_CANCEL_OPTION);
                    }
                }
            });
            /*
             *修改地址
             */
            jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                Address address = new Address();
                address.setName(jLabel3.getText());
                address.setAddress(jLabel1.getText());
                address.setPhone(jLabel5.getText());
                address.setUsername(username);
                new AddSite(jFrame,true,"修改地址",address);
                jPanel2=Tool工具.getAddressJPanel(jFrame,name);
                inJScrollPane1.setViewportView(jPanel2);
                // 加载并刷新面板
                inJScrollPane1.repaint();
                inJScrollPane1.updateUI();
                inJScrollPane1.revalidate();
            }
        });
    }
        jPanel.setPreferredSize(new Dimension(590,10+150*addresses.size()));
        return jPanel;
    }

    // 添加订单
    public static int setOrders(String username,String shopname,String pirce){
        Order order = new Order();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = df.format(new Date());
        order.setUsername(username);
        order.setShopname(shopname);
        order.setPirce(pirce);
        order.setTime(time);
        order.setState("F");
        OrderDao orderDao = new OrderDao();
        return orderDao.InsertOrder(order);
    }

    // 获取订单
    public static JPanel getJPanelOrders(JFrame jFrame){
        // 设置中部的滚动面板
        Tool工具 tool工具 = new Tool工具();
        OrderDao orderDao = new OrderDao();
        List<Order> orders =orderDao.queryOrder(tool工具.getName());

        jPanel2 = new JPanel();

        jPanel2.setPreferredSize(new Dimension(590, 770)); // 设定面板大小
        jPanel2.setLayout(null); // 取消布局
        if(orders.size()*150+10>770){
            jPanel2.setPreferredSize(new Dimension(590, orders.size()*150+10)); // 设定面板大小
        }
        for (int c = 0; c<orders.size();c++){
            JPanel jPanel1 = new JPanel();
            jPanel1.setBackground(Color.white);
            jPanel1.setBounds(0,10+150*c,590,140);
            jPanel1.setLayout(null);
            JLabel jLabel1 = new JLabel(orders.get(c).getShopname());
            jLabel1.setBounds(10,10,500,60);
            jLabel1.setFont(new Font("黑体",Font.BOLD,30));
            jPanel1.add(jLabel1);
            JLabel jLabel12 = new JLabel("删除",0);
            jLabel12.setBounds(510,10,80,60);
            jLabel12.setBorder(new MatteBorder(2,2,2,2,Color.GREEN));
            jLabel12.setFont(new Font("黑体",Font.BOLD,20));
            jLabel12.setCursor(new Cursor(Cursor.HAND_CURSOR));
            jPanel1.add(jLabel12);
            JLabel jLabel3 = new JLabel(orders.get(c).getTime());
            jLabel3.setBounds(10,70,400,60);
            jLabel3.setFont(new Font("黑体",Font.BOLD,30));
            jPanel1.add(jLabel3);
            JLabel jLabel5 = new JLabel("￥"+orders.get(c).getPirce());
            jLabel5.setBounds(410,70,100,60);
            jLabel5.setFont(new Font("黑体",Font.BOLD,20));
            jLabel5.setForeground(Color.red);
            jPanel1.add(jLabel5);
            jPanel2.add(jPanel1);
            String state = orders.get(c).getState();
            String Pirce = orders.get(c).getPirce();
            jLabel12.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount()==1){
                        Order order =  new Order();
                        order.setPirce(Pirce);
                        order.setShopname(jLabel1.getText());
                        order.setState(state);
                        order.setTime(jLabel3.getText());
                        order.setUsername(tool工具.getName());
                        int a=new OrderDao().DeleteOrder(order);
                        System.out.println(a);
                        if (a==1){
                            JPanel jPanel1 = new JPanel();
                            jPanel1=Tool工具.getJPanelOrders(jFrame);
                            inJScrollPane.setViewportView(jPanel1);
                            // 加载并刷新面板
                            inJScrollPane.repaint();
                            inJScrollPane.updateUI();
                            inJScrollPane.revalidate();
                            JOptionPane.showMessageDialog(jFrame, "删除成功", "成功", JOptionPane.OK_CANCEL_OPTION);
                        }else{
                            JOptionPane.showMessageDialog(jFrame, "删除失败", "失败", JOptionPane.OK_CANCEL_OPTION);
                        }
                    }
                }
            });
        }
        return jPanel2;
    }

    // 获取待评价
    public static JPanel getJPanelEvaluate(JFrame jFrame){
        // 设置中部的滚动面板
        Tool工具 tool工具 = new Tool工具();
        OrderDao orderDao = new OrderDao();
        List<Order> orders =orderDao.queryOrder1(tool工具.getName(),"F");

        jPanel2 = new JPanel();
        jPanel2.setPreferredSize(new Dimension(590, 770)); // 设定面板大小
        jPanel2.setLayout(null); // 取消布局
        if(orders.size()*150+10>770){
            jPanel2.setPreferredSize(new Dimension(590, orders.size()*150+10)); // 设定面板大小
        }
        for (int c = 0; c<orders.size();c++){
            JPanel jPanel1 = new JPanel();
            jPanel1.setBackground(Color.white);
            jPanel1.setBounds(0,10+150*c,590,140);
            jPanel1.setLayout(null);
            JLabel jLabel1 = new JLabel(orders.get(c).getShopname());
            jLabel1.setBounds(10,10,500,60);
            jLabel1.setFont(new Font("黑体",Font.BOLD,30));
            jPanel1.add(jLabel1);
            JLabel jLabel12 = new JLabel("评价",0);
            jLabel12.setBounds(510,10,80,60);
            jLabel12.setBorder(new MatteBorder(2,2,2,2,Color.GREEN));
            jLabel12.setFont(new Font("黑体",Font.BOLD,20));
            jLabel12.setCursor(new Cursor(Cursor.HAND_CURSOR));
            jPanel1.add(jLabel12);
            JLabel jLabel3 = new JLabel(orders.get(c).getTime());
            jLabel3.setBounds(10,70,400,60);
            jLabel3.setFont(new Font("黑体",Font.BOLD,30));
            jPanel1.add(jLabel3);
            JLabel jLabel5 = new JLabel("￥"+orders.get(c).getPirce());
            jLabel5.setBounds(410,70,100,60);
            jLabel5.setFont(new Font("黑体",Font.BOLD,20));
            jLabel5.setForeground(Color.red);
            jPanel1.add(jLabel5);
            jPanel2.add(jPanel1);
            Order order = orders.get(c);
            jLabel12.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if(e.getClickCount()==1){
                        new EvaluateFrame(jFrame,true,"评价",jLabel1.getText(),order);
                        jPanel2=getJPanelEvaluate(jFrame);
                        inJScrollPane.setViewportView(jPanel2);
                        // 加载并刷新面板
                        inJScrollPane.repaint();
                        inJScrollPane.updateUI();
                        inJScrollPane.revalidate();
                    }
                }
            });
        }
        return jPanel2;
    }

    // 获取商品图片
    public static JPanel getJpaneShop(String shop,String sort,JLabel jLabel5){
        List<Food> foods = new ArrayList<Food>();
        FoodDao foodDao = new FoodDao();
        foods = foodDao.queryFoodName(sort,shop);
        Tool工具 tool工具 = new Tool工具();
        JPanel jpanel = new JPanel();
        jpanel.setPreferredSize(new Dimension(590, 205*foods.size()));
        jpanel.setLayout(null);
        for (int i = 0; i < foods.size(); i++) {
            String name = foods.get(i).getName();
            JPanel jPanel2 = new JPanel();
            jPanel2.setBackground(Color.white);
            jPanel2.setLayout(null);
            jPanel2.setBounds(0, 205 * i, 600, 200);
            ImageIcon icon1 = new ImageIcon(foods.get(i).getPictureaddress());
            JLabel jLabel = new JLabel(icon1,0);
            jLabel.setBounds(0,0,200,200);
            jPanel2.add(jLabel);
            JLabel jLabel6 = new JLabel(foods.get(i).getName());
            jLabel6.setBounds(200,10,400,70);
            jLabel6.setFont(new Font("黑体",Font.BOLD,30));
            jPanel2.add(jLabel6);
            JLabel jLabel7= new JLabel("￥："+foods.get(i).getPrice());
            jLabel7.setBounds(200,80,400,70);
            jLabel7.setFont(new Font("黑体",Font.BOLD,30));
            jLabel7.setForeground(Color.red);
            jPanel2.add(jLabel7);
            jPanel2.setCursor(new Cursor(Cursor.HAND_CURSOR));
            jpanel.add(jPanel2);
            // 设置点击事件
            jPanel2.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount()==1){
                        tool工具.setMap(shop,sort,name,jLabel5);
                        setJLabel4(shop,sort);
                    }
                }
            });

        }
        return jpanel;
    }

    // 商家获取评价
    public static JPanel getJpaneEvaluate(String shopName){
        Tool工具 tool工具 = new Tool工具();
        List<Evaluate> evaluates = new ArrayList<Evaluate>();
        EvaluateDao evaluateDao = new EvaluateDao();
        evaluates = evaluateDao.queryEvaluates(shopName);
        JPanel jpanel = new JPanel();
        jpanel.setPreferredSize(new Dimension(590, 10+300*evaluates.size()));
        jpanel.setLayout(null);
        if (evaluates.size()!=0){
            for (int a=0;a<evaluates.size();a++){
                JPanel jPanel = new JPanel();
                jPanel.setBounds(0,10+300*a,590,300);
                jPanel.setLayout(null);
                JLabel jLabel = new JLabel("用户：");
                jLabel.setBounds(0,0,100,50);
                jLabel.setFont(new Font("黑体",Font.BOLD,20));
                jPanel.add(jLabel);

                JLabel jLabel1 = new JLabel(evaluates.get(a).getUsername());
                jLabel1.setBounds(100,0,290,50);
                jLabel1.setFont(new Font("黑体",Font.BOLD,30));
                jPanel.add(jLabel1);

                JLabel jLabel2 = new JLabel(evaluates.get(a).getStar());
                jLabel2.setBounds(390,0,250,50);
                jLabel2.setFont(new Font("黑体",Font.BOLD,30));
                jPanel.add(jLabel2);

                TextArea textArea = new TextArea(evaluates.get(a).getContent());
                textArea.setBounds(10,60,570,190);
                textArea.setFont(new Font("黑体",Font.BOLD,20));
                textArea.setEditable(false);
                jPanel.add(textArea);

                JLabel jLabel3 = new JLabel("评价时间："+evaluates.get(a).getTime(),0);
                jLabel3.setBounds(0,250,590,30);
                jLabel3.setForeground(Color.GRAY);
                jPanel.add(jLabel3);
                jpanel.add(jPanel);

            }
        }else {
            JLabel jLabel = new JLabel("这家店铺还有用户评论",0);
            jLabel.setBounds(0,10,590,50);
            jLabel.setForeground(Color.GRAY);
            jLabel.setFont(new Font("黑体",Font.BOLD,20));
            jpanel.add(jLabel);
        }
        return jpanel;
    }

    // 获取验证码
    public static int getSendSms(){
        Random random = new Random();

        return random.nextInt(899999)+100000;
    }

    // 发送短信
    public static void SendSms(String phoneNumber,int the){
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI4G8AEJpuUu69U3ZUakvv", "WAwppA0bvPybuyUHsAUyxFWcJPMzVo");
        //DefaultProfile profile = DefaultProfile.getProfile("default", "", "");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "default");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", "吃了没验证码");
        request.putQueryParameter("TemplateCode", "SMS_195575807");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+the+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    //获取图片的地址
    public static File getURL(){
        JFileChooser jFileChooser = new JFileChooser();
        if (jFileChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
            File file = jFileChooser.getSelectedFile();
            return file;
        }else
            return null;
    }


}
